//
//  BusinessDataRequestManager.m
//  etionMisc
//
//  Created by wjxfhxy on 1/28/14.
//  Copyright (c) 2014 GuangZhouXuanWu. All rights reserved.
//

#import "XMSBusinessDataRequestManager.h"

@implementation XMSBusinessDataRequestManager

@synthesize progressViewReleaseDelayTime = _progressViewReleaseDelayTime;
@synthesize maskOffProgressView = _maskOffProgressView;

-(id<XMSBusinessDataRequestManagerDelegate>)delegate
{
    return (id<XMSBusinessDataRequestManagerDelegate>)_delegate;
}

#pragma mark ----

- (void)progressViewBegin:(NSString *)szFuncationname
{
    if (NO == _maskOffProgressView && [self.delegate respondsToSelector:@selector(beginProgressView:funcationName:)])
    {
        id <XMSBusinessDataRequestManagerDelegate> delegate = self.delegate;
        DispatchMainThreadAsync(^
        {
            [delegate beginProgressView:self funcationName:szFuncationname];
        });
    }
}

- (void)progressViewEnd:(NSString *)szFuncationname
{
    if (NO == _maskOffProgressView && [self.delegate respondsToSelector:@selector(endProgressView:funcationName:)])
    {
        id <XMSBusinessDataRequestManagerDelegate> delegate = self.delegate;
        DispatchMainThreadAsync(^
        {
            [delegate endProgressView:self funcationName:szFuncationname];
        });
    }
}

- (void)progressViewBegin
{
    if (NO == _maskOffProgressView && [self.delegate respondsToSelector:@selector(beginProgressView:)])
    {
        id <XMSBusinessDataRequestManagerDelegate> delegate = self.delegate;
        DispatchMainThreadAsync(^
        {
            [delegate beginProgressView:self];
        });
    }
}

- (void)progressViewEnd
{
    if (NO == _maskOffProgressView && [self.delegate respondsToSelector:@selector(endProgressView:)])
    {
        id <XMSBusinessDataRequestManagerDelegate> delegate = self.delegate;
        DispatchMainThreadAsync(^
        {
            [delegate endProgressView:self];
        });
    }
}

- (void)progressViewReleaseDelay
{
    if (NO == _maskOffProgressView && [self.delegate respondsToSelector:@selector(holdProgressView:delayTime:)] && _progressViewReleaseDelayTime > 0.0)
        [self.delegate holdProgressView:self delayTime:_progressViewReleaseDelayTime];
}

- (void)progressViewReleaseDelay:(NSTimeInterval)nDelaytime handle:(void (^)())handle
{
    _progressViewReleaseDelayTime = nDelaytime;
    handle();
    _progressViewReleaseDelayTime = 0;
}

#pragma mark ----

- (void)progressViewBeginWithTip:(NSString*)tip
{
    if (NO == _maskOffProgressView && [self.delegate respondsToSelector:@selector(beginProgressViewWithTip:)])
    {
        id <XMSBusinessDataRequestManagerDelegate> delegate = self.delegate;
        DispatchMainThreadAsync(^
        {
            [delegate beginProgressViewWithTip:tip];
        });
    }
}

#pragma mark ----

- (void)pullRefreshViewBegin
{
    if (NO == _maskOffProgressView && [self.delegate respondsToSelector:@selector(beginPullRefreshView:)])
    {
        id <XMSBusinessDataRequestManagerDelegate> delegate = self.delegate;
        DispatchMainThreadAsync(^
        {
            [delegate beginPullRefreshView:self];
        });
    }
}

- (void)pullRefreshViewBegin:(NSString *)szFuncationname
{
    if (NO == _maskOffProgressView && [self.delegate respondsToSelector:@selector(beginPullRefreshView:funcationName:)])
    {
        id <XMSBusinessDataRequestManagerDelegate> delegate = self.delegate;
        DispatchMainThreadAsync(^
        {
            [delegate beginPullRefreshView:self funcationName:szFuncationname];
        });
    }
}

- (void)pullRefreshViewEnd
{
    if (NO == _maskOffProgressView && [self.delegate respondsToSelector:@selector(endPullRefreshView:)])
    {
        id <XMSBusinessDataRequestManagerDelegate> delegate = self.delegate;
        DispatchMainThreadAsync(^
        {
            [delegate endPullRefreshView:self];
        });
    }
}

- (void)pullRefreshViewEnd:(NSString *)szFuncationname
{
    if (NO == _maskOffProgressView && [self.delegate respondsToSelector:@selector(endPullRefreshView:funcationName:)])
    {
        id <XMSBusinessDataRequestManagerDelegate> delegate = self.delegate;
        DispatchMainThreadAsync(^
        {
            [delegate endPullRefreshView:self funcationName:szFuncationname];
        });
    }
}

#pragma mark ----

- (void)pullLoadMoreViewBegin
{
    if (NO == _maskOffProgressView && [self.delegate respondsToSelector:@selector(beginPullLoadMoreView:)])
    {
        id <XMSBusinessDataRequestManagerDelegate> delegate = self.delegate;
        DispatchMainThreadAsync(^
        {
            [delegate beginPullLoadMoreView:self];
        });
    }
}

- (void)pullLoadMoreViewBegin:(NSString *)szFuncationname
{
    if (NO == _maskOffProgressView && [self.delegate respondsToSelector:@selector(beginPullLoadMoreView:funcationName:)])
    {
        id <XMSBusinessDataRequestManagerDelegate> delegate = self.delegate;
        DispatchMainThreadAsync(^
        {
            [delegate beginPullLoadMoreView:self funcationName:szFuncationname];
        });
    }
}

- (void)pullLoadMoreViewEnd
{
    if (NO == _maskOffProgressView && [self.delegate respondsToSelector:@selector(endPullLoadMoreView:)])
    {
        id <XMSBusinessDataRequestManagerDelegate> delegate = self.delegate;
        DispatchMainThreadAsync(^
        {
            [delegate endPullLoadMoreView:self];
        });
    }
}

- (void)pullLoadMoreViewEnd:(NSString *)szFuncationname
{
    if (NO == _maskOffProgressView && [self.delegate respondsToSelector:@selector(endPullLoadMoreView:funcationName:)])
    {
        id <XMSBusinessDataRequestManagerDelegate> delegate = self.delegate;
        DispatchMainThreadAsync(^
        {
            [delegate endPullLoadMoreView:self funcationName:szFuncationname];
        });
    }
}

#pragma mark ----

//- (void)requestCompletion:(NSString *)szFuncationname result:(NSInteger)nResult output:(id)output error:(NSError *)error
//{
//    if ([self.delegate respondsToSelector:@selector(requestComplete:funcationName:result:output:error:)] == YES && self.isRejectRequest != YES)
//    {
//        XMSDataOperationManager *dom = self;
//        NSString *szF = szFuncationname;
//        id otpt = output;
//        NSError *e = error;
//        id <XMSBusinessDataRequestManagerDelegate> delegate = self.delegate;
//        NSInteger nR = nResult;
//        DispatchMainThreadAsync(^
//        {
//            [delegate requestComplete:dom funcationName:szF result:nR output:otpt error:e];
//        });
//    }
//}

- (void)requestResult:(NSInteger)nResult output:(id)output error:(NSError *)error completion:(void (^)(NSInteger, id, NSError *))completion
{
    if (self.isRejectRequest != YES)
    {
        XMSDataOperationManager *dom = self;
        id otpt = output;
        NSError *e = error;
        NSInteger nR = nResult;
        DispatchMainThreadAsync(^
        {
            if(self.isRejectRequest != YES)
            {
                (void) dom;
                completion(nR, otpt, e);
            }
        });
    }
}

- (void)requestResult:(NSInteger)nResult bResult:(BOOL)bResult error:(NSError *)error completion:(void (^)(NSInteger nResult, BOOL bResult, NSError *error))completion
{
    if (self.isRejectRequest != YES)
    {
        XMSDataOperationManager *dom = self;
        BOOL otpt = bResult;
        NSError *e = error;
        NSInteger nR = nResult;
        DispatchMainThreadAsync(^
        {
            if(self.isRejectRequest != YES)
            {
                (void) dom;
                completion(nR, otpt, e);
            }
        });
    }
}

- (void)requestResult:(NSInteger)nResult output:(id)output completion:(void (^)(NSInteger nResult, id output))completion
{
    [self requestResult:nResult output:output error:nil completion:^(NSInteger nResult, id output, NSError *error)
    {
        completion(nResult,output);
    }];
}

- (void)requestResult:(NSInteger)nResult bResult:(BOOL)bResult completion:(void (^)(NSInteger nResult, BOOL bResult))completion
{
    [self requestResult:nResult bResult:bResult error:nil completion:^(NSInteger nResult, BOOL bResult, NSError *error)
    {
        completion(nResult,bResult);
    }];
}

@end
